package com.study.lambda.basedemo;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/6/30 20:58
 */
public class FunctionDemo
{
    public static void main(String[] args)
    {
        // consumer(20, x -> System.out.println("x = " + x));
        // Random ran = new Random();
        // List<Integer> list = supplier(5, () -> ran.nextInt(10));
        // list.forEach(e -> System.out.println("e = " + e));
        //
        // Integer s = strOperator("1234 ", x -> Integer.valueOf(x.substring(0, 2)));
        // System.out.println(s);

        List<Integer> l = new ArrayList<>();
        l.add(102);
        l.add(172);
        l.add(13);
        l.add(82);
        l.add(109);
        List<Integer> list1 = filterInt(l, x -> (x > 100));
        for (Integer integer : list1)
        {
            System.out.println(integer);
        }
    }

    public static void consumer(double money, Consumer<Double> c)
    {
        c.accept(money);
    }

    public static List<Integer> supplier(int sum, Supplier<Integer> sup)
    {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < sum; i++)
        {
            list.add(sup.get());
        }
        return list;
    }

    public static Integer strOperator(String str, Function<String, Integer> fun)
    {
        return fun.apply(str);
    }

    public static List<Integer> filterInt(List<Integer> list, Predicate<Integer> pre)
    {
        List<Integer> l = new ArrayList<>();
        for (Integer integer : list)
        {
            if (pre.test(integer))
                l.add(integer);
        }
        return l;
    }
}
